
var Template = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.dept = form.dept.value;
				body.name = form.name.value;			
				body.content = form.content.value;

				$(this).dialog("close");

				$.post('kq.php?do=Template.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		$.cookie('status_', status);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'kq.php?do=Template.edit&id=' + id;
		var c2 = 'kq.php?do=Template.view&id=' + id;
		var c3 = 'kq.php?do=Template.copy&id=' + id;

		var c4 = 'kq.php?do=TemplatePlan.findPost&template_s=' + id;
		var c5 = 'kq.php?do=TemplatePlan.batch&id=' + id;
		
		var c6 = 'kq.php?do=Plan.template&id=' + id;

		var a1 = ['编辑信息', 'url', 'Template.dialog_edit_', '600x340'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '600x340'];
		var a3 = ['复制信息', 'url', 'Template.dialog_copy_', '600x340'];

		var a4 = ['排班信息', 'url', 'list' ];
		var a5 = ['生成排班', 'url', 'Template.dialog_plan_', '600x400'];
		
		var a6 = ['生成正班', 'url', 'Template.dialog_emp_', '600x500'];

		var s = c1 + ", " + c2 + ", " + c3 + ", " + c4 + ", " + c5 + ", " + c6;

		return popup(e, [ a1, a2, a3, a4, a5, a6], s);
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('kq.php?do=Template.ajax_oper', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {			
					"关闭" : function() {
						$(this).dialog("close");
					},
			
					"修改" : function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
			
						var form = $("#form_edit")[0];
			
						var body = {};
						body.id = form.id.value;
						body.dept = form.dept.value;
						body.name = form.name.value;			
						body.content = form.content.value;
						body.status = form.status.value;
			
						$(this).dialog("close");
			
						var url = 'kq.php?do=Template.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
			
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});
			
				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭" : function() {
				$(this).dialog("close");
			},

			"复制" : function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = {};
				body.name = form.name.value;
				body.dept = form.dept.value;
				body.content = form.content.value;

				$(this).dialog("close");

				$.post('kq.php?do=Template.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	dialog_plan_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭" : function() {
				$(this).dialog("close");
			},

			"生成" : function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = {};
				body.id = form.id.value;

				body.date1 = form.date1.value;
				body.date2 = form.date2.value;

				body.time1 = form.time1.value;
				body.time2 = form.time2.value;
				
				var week = new Array();
				$("input[name^='week_']").each(function() {
					if ($(this).attr("checked")) {
						week.push($(this).val());
					}
				})
				
				// alert($.toJSON(week));
				body.week = $.toJSON(week);

				body.holiday = form.holiday.value;
				body.content = form.content.value;

				$(this).dialog("close");

				$.post('kq.php?do=TemplatePlan.batchPost', body, function(data) {
					alert("生成成功！");
				});
			}
		});

		dialog_add(url, title, xy);
	},

	dialog_emp_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"提交": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var foo = [];
				$('#emp option').each(function(i, n) {
					foo[i] = $(n).val();
				});

				var form = $("#form_add")[0];

				var body = { };
				body.id = form.id.value;
				body.list = foo.join(",");

				$(this).dialog("close");

				var url = 'kq.php?do=Plan.templatePost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					alert("操作成功！");
				});
			}
		});

		dialog_add(url, title, xy);
	}
};
